<?php

/***************************************************************\
* FILE:		sys/pages/subset.inc
* SECTION:	pages > core set
* FUNCTION:	give an overview of the contents of a subset
* USES:	
* INCLUDES:		
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
}

/**
 *
 */
function page_inc_execute_action($action) {
	global $PAGE;
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];

	switch($action) {
		case "markread":
			if (!$_SESSION['user_id'])
				errorpage(6);
			$from = array("ITEM_IN", "DUMMY");
			$where = "ITEM_IN.sub_id = {$PAGE['id']}";
			$sql = db_query_select(array(array(), "ITEM_IN.item_id"), $from, $where);
			$sqlresult = db_do_query($sql);
			$values = array();
			while ($r = db_fetch_array($sqlresult))
				array_push($values, array($r['item_id'], $_SESSION['user_id']));
			$sql = db_query_insert_multiple(array(item_id, user_id), $values, "HAS_READ", 1);
			db_do_query($sql);
			// TODO crossposted dirs should be updated as well
			$n = db_affected_rows();
			$PAGE['menu'][$THISDIR['dir_id']]['new_items'] -= $n;
			$PAGE['menu'][$THISDIR['dir_id']]['read_items'] += $n;
			add_alert("alles gelezen hier...");
			break;
		default:
			add_alert("unknown action: $action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE, $CONFIG;
	$CONTENT = array();
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];
	
	$CONTENT['items'] = array();

	$where = "ITEM.item_type_id = ITEM_TYPE.item_type_id AND \n\t";
	$where .= "ITEM.item_id = ITEM_IN.item_id AND \n\t";
	$where .= "ITEM_IN.sub_id = {$PAGE['id']} AND \n\t";
	$where .= "ITEM.author = M1.user_id AND \n\t";
	$where .= "ITEM.editor = M2.user_id AND \n\t";
	$where .= "NOT ITEM_IN.deleted AND \n\t";
	$where .= "NOT ITEM.deleted";

	$order = "sticky DESC, {$THISDIR['sorting_sql']}";

	$sql = db_query_select(array(array("*"), array("*"), array(), array(), array(), "M1.nick as nick_author, M2.nick as nick_editor"), array("ITEM", "ITEM_TYPE", "ITEM_IN", "MEMBER AS M1", "MEMBER AS M2", "DUMMY"), $where, $order);
	$items = db_do_query($sql);

	$read = array();
	if ($_SESSION['user_id']) {
		$readings = db_do_query(db_query_select(array(array(), "HAS_READ.item_id"), array("(((HAS_READ INNER JOIN ITEM ON HAS_READ.item_id = ITEM.item_id) INNER JOIN ITEM_IN ON HAS_READ.item_id = ITEM_IN.item_id) INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id)" . ($THISDIR['show_subsets'] ? " INNER JOIN SUBSET ON SUB_IN.sub_id = SUBSET.sub_id" : "")), "SUB_IN.dir_id = {$THISDIR['dir_id']} AND user_id = {$_SESSION['user_id']}", $order, $limit));
		while ($item = db_fetch_array($readings))
			$read[$item['item_id']] = 1;
	}

	while ($item = db_fetch_array($items)) {
		$item['unread'] = $_SESSION['user_id'] ? !$read[$item['item_id']] : 0;
		$item['new'] = (($item['edit'] > $_SESSION['member']['last_visit']) and $item['unread']) ? 1 : 0;
		array_unshift($CONTENT['items'], $item);
	}

	if ($_SESSION['user_id']) {
		
		// mark all items read
		add_action('markread', 'url-action', 'markread');
	
		// adding
		if (has_access("ADD"))
			add_action('add_item', 'url', ".add_item?sub_id={$PAGE['id']}");
			
		// edit subset settings
		if (has_access("MODERATE") or has_access("ADMIN"))
			add_action('edit_sub', 'url', "{$PAGE['id']}.edit_sub");
	}
	
	$sql = db_query_update(array('hits'), array('hits + 1'), "DIRECTORY", "dir_id = {$THISDIR['dir_id']}");
	db_do_query($sql);

	$sql = db_query_select(array(array('dir_id'), array(), "BODY.body, SUB_IN.sub_title, SUBSET.sub_id"), array("SUB_IN", "SUBSET LEFT JOIN BODY ON SUBSET.body_id = BODY.body_id", "DUMMY"), "SUB_IN.dir_id = {$THISDIR['dir_id']} AND SUB_IN.sub_id = SUBSET.sub_id AND SUBSET.sub_id = {$PAGE['id']}");
	$sqlresult = db_do_query($sql);
	$CONTENT['subset_indirs'] = array();
	while ($s = db_fetch_array($sqlresult)) {
		$CONTENT['subset'] = $s;
		$CONTENT['subset_indirs'][] = $s['dir_id'];
	}
	
	$CONTENT['title'] = $CONTENT['subset']['sub_title'];
		
	$PAGE['content'] = $CONTENT;
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_body($PAGE['content']['subset']['body']);
	print_itemlist($PAGE['content']['items'], $PAGE['dirpath'], 0);
}

?>